package com.yw.atp.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.yw.atp.Constant;


@Entity
@Table(name=Constant.DB_PREFIX+"staff")
public class Staff implements IDEntity{
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue
    private Integer id;
	private String name;
	@Column(unique=true,nullable=false)
	private String mobile;
	@Column(unique=true,nullable=false)
	private String mobileID;
	private String special;
	@ManyToOne
	private Department department;
	
	@ManyToMany
	@JoinTable(name=Constant.DB_PREFIX+"staff_profession",
			joinColumns=@JoinColumn(name="staff_id",referencedColumnName="id"),
			inverseJoinColumns=@JoinColumn(name="profession_id",referencedColumnName="id"))
	private Set<Profession> professions;
	
	@ManyToMany(mappedBy="staffList")
    private Set<Command> commandSet;
	
	@Temporal(TemporalType.TIMESTAMP)
	@OrderBy("createdAt desc")
	private Date createdAt;
	@OneToMany(mappedBy="issuer",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private Set<Command> issueCommands;
	@OneToMany(mappedBy="sender",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private Set<Command> sendCommands;
	@OneToMany(mappedBy="owner",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
    private Set<Reply> replyList;
	private String status = "0";
	private Date updateAt;
	public Date getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}
	
	public String getMobileID() {
		return mobileID;
	}

	public void setMobileID(String mobileID) {
		this.mobileID = mobileID;
	}
	
	public String getSpecial() {
		return special;
	}

	public void setSpecial(String special) {
		this.special = special;
	}
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public Set<Reply> getReplyList() {
		return replyList;
	}

	public void setReplyList(Set<Reply> replyList) {
		this.replyList = replyList;
	}

	public Set<Command> getIssueCommands() {
		return issueCommands;
	}

	public void setIssueCommands(Set<Command> issueCommands) {
		this.issueCommands = issueCommands;
	}

	public Set<Command> getSendCommands() {
		return sendCommands;
	}

	public void setSendCommands(Set<Command> sendCommands) {
		this.sendCommands = sendCommands;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	
	public Set<Command> getCommandSet() {
		return commandSet;
	}

	public void setCommandSet(Set<Command> commandSet) {
		this.commandSet = commandSet;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public Set<Profession> getProfessions() {
		return professions;
	}

	public void setProfessions(Set<Profession> professions) {
		this.professions = professions;
	}
	
	public static JSONArray capture(List<Staff> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Staff appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public static JSONArray capture(Set<Staff> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Staff appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		appm.put("name",name);
		appm.put("mobile",mobile);
		appm.put("special",special);
		appm.put("mobileID",mobileID);
		if(department!=null){
			appm.put("department",department.capture());
		}
		if(professions!=null){
			List<Profession> proList = new ArrayList<Profession>();
			proList.addAll(professions);
			appm.put("professions",Profession.capture(proList));
		}
		return appm;
	}
    
	public static JSONArray captureForSel(List<Staff> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Staff appm : appms){
			jsonAppms.put(appm.captureForSel());
		}
		return jsonAppms;
	}
	
	public JSONObject captureForSel() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		appm.put("name",name);
		return appm;
	}
}
